//
//  CFOrderDetailViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/29.
//
//

#import "CFOrderDetailViewController.h"
#import "DetailInfoTableViewCell.h"
#import "NewCarPaymentInfoTableViewCell.h"
#import "SecondCarOrderDetailTableViewCell.h"
#import "ApplyViewController.h"

@interface CFOrderDetailViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *applyBtn;

@property (nonatomic, strong) NSArray *dataArr;
@end

@implementation CFOrderDetailViewController
#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 2 ? 6 : 1;
}

kRemobeCellSeparator
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
        Cell.textLabel.text = self.model.carTypeName;
        Cell.textLabel.font = [UIFont systemFontOfSize:17 * AUTO_WIDTH];
        Cell.textLabel.textAlignment = NSTextAlignmentCenter;
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return Cell;
    } else if (indexPath.section == 1) {
        if (self.model.carType == YDBCarTypeNewCar) {
            NewCarPaymentInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
            Cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return Cell;
        } else {
            SecondCarOrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondCarCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    } else {
        DetailInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell"];
        Cell.textLabel.text = self.dataArr[indexPath.row];
        Cell.textLabel.font = [UIFont systemFontOfSize:17 * AUTO_WIDTH];
        Cell.infoLB.text = @"123456";
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return Cell;
    }
}

#pragma mark - UITableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.model.carType == YDBCarTypeNewCar) {
        return (indexPath.section == 2 || indexPath.section == 0) ? 44 : 120 * AUTO_WIDTH;
    } else {
        return (indexPath.section == 2 || indexPath.section == 0) ? 44 : 180 * AUTO_WIDTH;
    }
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.title = (self.model.carType == YDBCarTypeNewCar ? @"订单详情" : @"二手车贷款");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 方法 methods
- (void)applyNow:sender {
    ApplyViewController *vc = [ApplyViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(0);
            make.bottom.equalTo(self.applyBtn.mas_top).equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"titleCell"];
        [_tableView registerClass:[NewCarPaymentInfoTableViewCell class] forCellReuseIdentifier:@"infoCell"];
        [_tableView registerClass:[DetailInfoTableViewCell class] forCellReuseIdentifier:@"detailCell"];
        [_tableView registerClass:[SecondCarOrderDetailTableViewCell class] forCellReuseIdentifier:@"secondCarCell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSArray *)dataArr {
    if(_dataArr == nil) {
        _dataArr = @[@"贷款额度 (元)",
                     @"保证金 (元)",
                     @"还款方式",
                     @"月供 (元)",
                     @"月利率",
                     @"总利息 (元)"];
    }
    return _dataArr;
}

- (UIButton *)applyBtn {
    if(_applyBtn == nil) {
        _applyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_applyBtn addTarget:self action:@selector(applyNow:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_applyBtn];
        [_applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.height.equalTo(44);
        }];
        [_applyBtn setTitle:@"立即申请" forState:UIControlStateNormal];
        _applyBtn.backgroundColor = kRGBA(0, 0, 0, 0.8);
        _applyBtn.titleLabel.font = [UIFont systemFontOfSize:17 * AUTO_WIDTH];
    }
    return _applyBtn;
}

@end
